﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using IFactory;
public partial class UserPlanMonth_ResolveMonthPlanToUserMonthPlan : System.Web.UI.Page
{
    Authentication dllAuthentication = new Authentication();
    BLL.UserPlanMonth bllUserPlanMonth = new UserPlanMonth();
    BLL.UserInfoManager dllUserInfoManager = new UserInfoManager();
    BLL.MonthPlanManager dllMonthPlanManager = new MonthPlanManager();
    Model.UserInfo objUserInfo = new Model.UserInfo();
    protected void Page_Load(object sender, EventArgs e)
    {
        dllAuthentication.isLogin();
        if (!IsPostBack)
        {
            bindDropDownList(this.drpMasterPeople);
            Bind();
        }
    }


    /// <summary>
    /// 绑定所有的dropdownlist
    /// </summary>
    /// <param name="_drp"></param>
    void bindDropDownList(DropDownList _drp)
    {
        if (_drp.Equals(this.drpMasterPeople))
        {
            this.drpMasterPeople.Items.Clear();
            object[] _value = new object[1];
            _value[0] = HttpUtility.UrlDecode(Request.QueryString["_DepartmentName"]);
            DataTable _userInfo = dllUserInfoManager.getUserInfoByDepartmentName(_value);
            if (_userInfo.Rows.Count > 0)
            {
                this.drpMasterPeople.DataSource = _userInfo;
                this.drpMasterPeople.DataTextField = "RealName";
                this.drpMasterPeople.DataValueField = "UserId";
                this.drpMasterPeople.DataBind();
                ListItem _selectLi = new ListItem("请选择员工", "0");
                this.drpMasterPeople.Items.Insert(0, _selectLi);
            }
            else
            {
                this.drpMasterPeople.Items.Clear();
                ListItem _li = new ListItem("该部门无员工", "0");
                this.drpMasterPeople.Items.Add(_li);
            }
        }
    }

    /// <summary>
    /// 绑定计划
    /// </summary>
    void Bind()
    {
        int MonthPlanId = Utility.HttpHelp.getParamsInt("_MonthPlanId");
        object[] _value = new object[1];
        _value[0] = MonthPlanId;
        this.MonthPlanId.Value = MonthPlanId.ToString();
        DataTable dt = dllMonthPlanManager.get(MonthPlanManager.field.MonthPlanId.ToString(), _value);
        if (dt.Rows.Count > 0)
        {
            this.txtCompleteTime.Text =Convert.ToDateTime(dt.Rows[0]["CompleteTime"].ToString()).ToShortDateString();

            string DepartmentManager = string.Empty;
            BLL.UserInfoManager bllUserInfoManager = new UserInfoManager();
            object [] _depart = new object[1];
            _depart[0] = objUserInfo.UserName;
            DataTable dt_departmentmanager =  bllUserInfoManager.get(UserInfoManager.field.IsDepartmentManager.ToString(),_depart);
            if (dt_departmentmanager.Rows.Count > 0)
            {
                this.txtEvaluatePeople.Value = dt_departmentmanager.Rows[0]["RealName"].ToString();
            }
            
            this.txtMasterDepartment.Text = dt.Rows[0]["MasterDepartment"].ToString();
            this.txtPlanStep.Text = dt.Rows[0]["PlanStep"].ToString();
            this.txtMonthPlanSortId.Value = dt.Rows[0]["MonthPlanSortId"].ToString();
            this.txtRemark.Text = dt.Rows[0]["Remart"].ToString();
            if (this.txtRemark.Text == "&amp;nbsp;" || this.txtRemark.Text == "&nbsp;")
            {
                this.txtRemark.Text = "";
            }
            this.txtSlaveDepartment.Value = dt.Rows[0]["SlaveDepartment"].ToString();
            this.txtSlaveDepartmentDuty.Text = dt.Rows[0]["SlaveDepartmentDuty"].ToString();
            this.txtWorkItem.Text = dt.Rows[0]["WorkItem"].ToString();
            this.HMonthDate.Value = dt.Rows[0]["MonthDate"].ToString();
            this.HVerifyCircuitId.Value = dt.Rows[0]["VerifyCircuitId"].ToString();
            this.HSource.Value = HttpUtility.UrlDecode(Request.QueryString["_Source"].ToString()) + " 第" + dt.Rows[0]["MonthPlanSortId"].ToString() + "条";
        }
    }


    /// <summary>
    /// 保存为员工月计划
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (this.drpMasterPeople.SelectedItem.Text == "请选择员工")
        {
            Utility.HttpHelp.alert("请选择员工！",this.Page);
            return;
        }
        if (this.txtSlaveDepartment.Value.Trim() != "独立完成" && this.txtSlaveDepartment.Value.Trim() != "全部部门")
        {
            if (this.txtSlaveDepartmentDuty.Text.Trim().Length < 18)
            {
                Utility.HttpHelp.alert("配合部门不得少于18个字节！", this.Page);
                return;
            }
        }
        //if (this.txtPlanStep.Text.Trim().Length == 0)
        //{
        //    Utility.HttpHelp.alert("请填写实施步骤!", Page);
        //    return;
        //}
        object[] _value = new object[1];
        _value[0] = this.drpMasterPeople.SelectedItem.Text;
        dllUserInfoManager.get(UserInfoManager.field.RealName.ToString(), _value);
        _value = new object[17];
        //完成时间
        _value[0] = Convert.ToDateTime(this.txtCompleteTime.Text.Trim());
        //工作内容
        _value[1] = this.txtWorkItem.Text.Trim();
        //计划实施步骤
        _value[2] = this.txtPlanStep.Text.Trim();
        //责任部门
        _value[3] = this.txtMasterDepartment.Text.Trim();
        //责任人
        _value[4] = this.drpMasterPeople.SelectedItem.Text;
        //配合部门
        _value[5] = this.txtSlaveDepartment.Value;
        //配合部门责任
        _value[6] = this.txtSlaveDepartmentDuty.Text;
        _value[7] = 3;//发送状态(部门月分解到员工月直接为待评估状态)
        _value[8] = 0;//是否达成共识
        //备注
        _value[9] = this.txtRemark.Text;//Remart
        //月周期
        _value[10] = Convert.ToInt32(this.HMonthDate.Value);//MonthDate
        //审核流程
        _value[11] = Convert.ToInt32(this.HVerifyCircuitId.Value);
        //发起人
        DataTable dt = dllUserInfoManager.get(UserInfoManager.field.RealName.ToString(), new object[1] { this.drpMasterPeople.SelectedItem.Text });
        if (dt.Rows.Count > 0)
        {
            _value[12] = dt.Rows[0]["UserName"].ToString();
        }
        else
        {
            _value[12] = "";
        }
        //评估人
        _value[13] = this.txtEvaluatePeople.Value;
        //来源
        _value[14] = this.HSource.Value;
        //部门月计划Id
        _value[15] = Utility.HttpHelp.getParamsInt("_MonthPlanId");
        //用户月计划排序id
        _value[16] =  this.txtMonthPlanSortId.Value ;

        if (bllUserPlanMonth.add(_value) > 0)
        {
        //    Utility.HttpHelp.alert("分解成功！", this.Page);
        //    Utility.HttpHelp.closeWindowNoFresh("UserPlanMonthList.aspx", this.Page);
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('分解成功！');self.close();</script>");
        }
        else
        {
            Utility.HttpHelp.alert("分解失败！", this.Page);
        }
    }
}
